import {Component, OnInit, Input, Injector} from '@angular/core';
import { RecruitmentPlan } from '../../../domains/recruitmentplan.domain';
import { RecruitmentPlanMgeSvrService} from '../../../services/recruitment-plan-mge-svr.service';
import { DeptServiceSvr } from '../../../services/dept.service';
import {CoreComponent} from 'pengesoft-ng-lib';
import {Department} from '../../../domains/department.domain';
import {JobServiceSvr} from '../../../services/job.service';

@Component({
  selector: 'app-recruitment-application',
  templateUrl: './recruitment-application.component.html',
  styleUrls: ['./recruitment-application.component.less']
})


export class RecruitmentApplicationComponent extends CoreComponent implements OnInit {
  ishow1 = true;    // 控制组件的显示与隐藏
  ishow2 = false;
  optionType = 0;    // 组件复用时区别功能
  n = 0;          // 弹框类型
  // options = [];
  departResult = [];    // 部门信息
  isVisible1 = false;   // 修改弹框显示隐藏控制
  isVisible2 = false;   // 驳回理由弹框控制
  remark: string;      // 驳回理由
  public department = new Department();    // 实例一个部门对象
  infoLi = new RecruitmentPlan();        // 实例一个申请信息对象
  Deptdefault: any;          // 修改时的部门默认值处理
  Jobdefault: any;          // 修改时岗位默认值处理
  JobList: any;             // 所有岗位
  selectType: any;  // 条件查询
  selectStatus = [
    {label: '全部', value: 0},
    {label: '待事业部审核', value: 1},
    {label: '待人事部确认', value: 3},
    {label: '待总经理确认', value: 4},
    {label: '审核通过', value: 5},
    {label: '已驳回', value: 2},
  ];


  @Input() info: any;
  constructor(
    private injector: Injector,
    public departSvr: DeptServiceSvr,        // 部门信息服务
    public recruplanSvr: RecruitmentPlanMgeSvrService,      // 申请信息服务
    public jobSvr: JobServiceSvr,          // 岗位信息服务
  ) {
    super(injector);
  }

  // 修改招聘申请表信息
  update(data): void {
    this.infoLi = new RecruitmentPlan();
    this.infoLi.Id = data.RecruitmentPlan.Id;

    // 拿到部门信息
    this.departSvr.getAllMyDepartment().then( res => {
      this.departResult.push(this.deepClone(res));

      // 拿到其他信息
      // tslint:disable-next-line:no-shadowed-variable
      this.recruplanSvr.getDetailList(this.infoLi).then( res => {
        this.infoLi = res[0].RecruitmentPlan;
        this.Jobdefault = res[0].Job.Id;
        this.Deptdefault = res[0].DeptList.map(items => {
          return items.Name;
        });
        // // 拿到岗位信息
        // tslint:disable-next-line:no-shadowed-variable
        this.jobSvr.getJobByDept(res[0].RecruitmentPlan.DeptId + '').then( res => {
          // tslint:disable-next-line:forin
          for (const i in res){
            res[i].label = res[i].JobName;
            res[i].value = res[i].Id;
          }
          this.JobList = res;
          this.ishow2 = true;
          this.ishow1 = false;
          this.optionType = 2;
        }, err => {
          this.nzMessageSvr.error(err.Msg);
        });
      }, err => {
        this.nzMessageSvr.error(err.Msg);
      });
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }

  // 跳转添加界面
  addPage(): void{

    // 初始化数据
    this.JobList = [];
    this.Deptdefault = [];
    this.Jobdefault = [];
    this.infoLi = new RecruitmentPlan();

    // 获取部门信息
    this.departSvr.getAllMyDepartment().then(res => {
      // @ts-ignore
      this.departResult.push(this.deepClone(res));
      // this.departResult.push(res);
      this.ishow1 = false;
      this.ishow2 = true;
      this.optionType = 1;
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }

  // 返回
  back(): void{
    this.ishow1 = true;
    this.ishow2 = false;
    this.departResult = [];
    this.recruplanSvr.getDetailList(new RecruitmentPlan()).then( res => {
      this.info = res;
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }

  ngOnInit(): void {
  }

  // 条件查询
  onChangesStatus(value): void{
    this.selectType = value;
  }
  sendSelect(): void{
    this.infoLi.CheckStatus = this.selectType;
    this.recruplanSvr.getDetailList(this.infoLi).then( res => {
      this.info = res;
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }

  // 弹框部分
  warn(n): void{
    this.n = n;
    this.isVisible1 = true;
  }
  handleOk(): void {
    this.isVisible2 = false;
    this.isVisible1 = false;
  }

  handleCancel(): void {
    this.isVisible2 = false;
    this.isVisible1 = false;
  }

  // 递归修改数据结构，使其与级联组件结构一致
  deepClone(obj): void{
    obj.label = obj.DeptName;
    obj.value = obj.NodeId;
    obj.children = obj.ChildDepartment;
    if (obj.ChildDepartment !== []){
      // tslint:disable-next-line:forin
      for (const i in obj.ChildDepartment){
             this.deepClone(obj.ChildDepartment[i]);
           }
    }
    if (obj.ChildDepartment === []){
      obj.isLeaf = true;
    }
    return obj;
  }

  // 查看驳回原因
  check(data): void{
    this.remark = data.RecruitmentPlan.Remark;
    this.isVisible2 = true;
  }
}

